package com.neusoft.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.neusoft.dto.PiVocationsDto;
import com.neusoft.dto.VocationDto;
import com.neusoft.dto.VocationsDto;
import com.neusoft.mapper.EmpMapper;
import com.neusoft.mapper.VocationMapper;
import com.neusoft.model.Vocation;
import com.neusoft.service.VacationService;
import com.neusoft.utils.ServerResponse;
import com.neusoft.vo.VocationVo;
import com.neusoft.vo.VocationsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @AUTHOR 杜豪冰
 * @DATE 2025/1/14 14:36
 * @DESCRIBER
 */
@Service
public class VacationServiceImpl implements VacationService {
    @Autowired
    VocationMapper vocationMapper;
    @Autowired
    EmpMapper empMapper;
    @Override
    public ServerResponse requestVacation(VocationDto vocationDto) {
        Vocation vocation = new Vocation();
        vocation.setEmpId(vocationDto.getEmpId());
        vocation.setVocationTypeId(vocationDto.getVocationTypeId());
        vocation.setStartTime(vocationDto.getStartTime());
        vocation.setEndTime(vocationDto.getEndTime());
        vocation.setReason(vocationDto.getReason());
        vocation.setDays((int)calculateDaysDifference(vocationDto.getEndTime(), vocationDto.getStartTime()));
        Byte approveStatus = 1;
        vocation.setApproveStatus(approveStatus);
        vocation.setCreateTime(new Date());
        vocation.setUpdateTime(new Date());
        vocationMapper.insert(vocation);
        return ServerResponse.success("申请成功");
    }

    @Override
    public ServerResponse getVocations(VocationsDto vocationsDto) {
        if(vocationsDto.getOffset() == null){
            vocationsDto.setOffset(0);
        }
        if(vocationsDto.getLimit() == null){
            vocationsDto.setLimit(10);
        }
        VocationsVo vocationsVo = new VocationsVo();
        List<VocationVo> vocations = vocationMapper.getPositions(vocationsDto);
        for (int i = 0; i < vocations.size(); i++) {
            if(vocations.get(i).getApproveStatus().equals("1")){
                vocations.get(i).setApproveStatus("审批中");
            }
            if(vocations.get(i).getApproveStatus().equals("2")){
                vocations.get(i).setApproveStatus("已通过");
            }
            if(vocations.get(i).getApproveStatus().equals("3")){
                vocations.get(i).setApproveStatus("已驳回");
            }
        }
        QueryWrapper<Vocation> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("emp_id", vocationsDto.getEmpId());
        vocationsVo.setTotal(Math.toIntExact(vocationMapper.selectCount(queryWrapper)));
        vocationsVo.setVocations(vocations);
        return ServerResponse.success(vocationsVo);
    }

    @Override
    public ServerResponse getDeptVocations(VocationsDto vocationsDto) {
        if(vocationsDto.getOffset() == null){
            vocationsDto.setOffset(0);
        }
        if(vocationsDto.getLimit() == null){
            vocationsDto.setLimit(10);
        }
//        empMapper.selectById(vocationsDto.getEmpId()).getDeptId();
        VocationsVo vocations = new VocationsVo();
        vocations.setTotal(vocationMapper.getDeptPositionsCount(vocationsDto));
        vocations.setVocations(vocationMapper.getDeptPositions(vocationsDto));
        return ServerResponse.success(vocations);
    }

    @Override
    public ServerResponse updateVocationStatus(PiVocationsDto piVocationsDto) {
        List<Integer> id = piVocationsDto.getIds();
        Integer status = piVocationsDto.getStatus();
        for (int i = 0; i < id.size(); i++) {
            Vocation vocation = vocationMapper.selectOne(new QueryWrapper<Vocation>().eq("id",id.get(i)));
            vocation.setApproveStatus((byte) status.intValue());
            vocationMapper.updateById(vocation);
        }
        return ServerResponse.success("更改成功");
    }

    public static int calculateDaysDifference(Date date1, Date date2) {
        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTime(date1);
        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTime(date2);

        long milliseconds1 = calendar1.getTimeInMillis();
        long milliseconds2 = calendar2.getTimeInMillis();

        long diffInMilliseconds = Math.abs(milliseconds2 - milliseconds1);
        int diffInDays = (int)Math.ceil(diffInMilliseconds / (24 * 60 * 60 * 1000.0));

        return diffInDays;
    }
}
